package com.yociyy.auth.biz;

import cn.hutool.core.util.ObjectUtil;
import com.yociyy.admin.feign.RemoteUserService;
import com.yociyy.admin.model.entity.BaseUser;
import com.yociyy.security.authentication.YoCiUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户信息业务流程实现
 * 
 * @author: YoCiyy
 * @date: 2020/6/3
 */
@Slf4j
@Component
@AllArgsConstructor
public class UserDetailBiz implements UserDetailsService {

	private final RemoteUserService remoteUserService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 查询用户信息
		BaseUser user = remoteUserService.selectUserByAccount(username);
		if (ObjectUtil.isNull(user)) {
			throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
		}
		Set<String> permissions = new HashSet<>();
		permissions.add("sys:user:admin");
		return new YoCiUser(user.getId(), user.getDeptId(), permissions, user.getAccount(), user.getPassword(), true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("Role_admin"));
	}
}
